package com.wontube.smr.bo.sites.crunchyroll;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.smr.bo.constants.StandarDramaGenres;

/**
 * @author tanggf(2012-8-2)
 *
 */
public class CrunchyrollDramaGenre {
	private static Map<String, String> map = new HashMap<String, String>();
	
	static {
		//cdrama, jdrama, kdrama, sgdrama, action, comedy, crime, family, food, historical, horror, martial_arts, romance, thriller
		//map.put("cdrama", StandarDramaGenres.CHINIESE);
		//map.put("jdrama", StandarDramaGenres.JAPANESE);
		//map.put("kdrama", StandarDramaGenres.KOREAN);
		//map.put("sgdrama", StandarDramaGenres.SINGAPOREAN);
		map.put("action", StandarDramaGenres.ACTION);
		map.put("comedy", StandarDramaGenres.COMEDY);
		map.put("crime", StandarDramaGenres.CRIME);
		map.put("family", StandarDramaGenres.FAMILY);
		map.put("food", StandarDramaGenres.FOOD);
		map.put("historical", StandarDramaGenres.HISTORICAL);
		map.put("horror", StandarDramaGenres.HORROR);
		map.put("martial_arts", StandarDramaGenres.MARTIAL_ARTS);
		map.put("romance", StandarDramaGenres.ROMANCE);
		map.put("thriller", StandarDramaGenres.THRILLER);		
	}
	
	/**
	 * 修正单个genre
	 * @param genreName
	 * @return
	 */
	public static String remendGenre(String genreName){
		if(StringUtils.isNullOrEmpty(genreName)){
			return null;
		}
		
		//处理专有genre, 不存在则判断是标准genre
		genreName = genreName.trim();
		String genre = map.get(genreName);
		return genre != null ? genre : genreName;
	}
	
	/**
	 * 处理全部genre,并防止重复
	 * @param genreNames (逗号分隔)
	 * @return
	 */
	public static String remendAllGenres(String genreNames){
		if(StringUtils.isNullOrEmpty(genreNames)){
			return null;
		}
		
		Set<String> genreSet = new HashSet<String>();		
		String[] genres = genreNames.split("\\s*,\\s*");
		for (String g : genres) {
			String rg = remendGenre(g);
			if(rg != null){
				genreSet.add(rg);
			}		
		}
		
		return ArrayUtils.join(genreSet, ",");
	}
	
	/**
	 * Crunchyroll 内部使用的genres
	 */
	public static Set<String> getInitDramaGenres(){
		return map.keySet();
	}
}
